package org.vector.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.table.DefaultTableModel;

import org.vector.daoImpl.BookDaoImpl;
import org.vector.serviceImpl.BookServiceImpl;

@SuppressWarnings("serial")
public class WinReadertypeset extends javax.swing.JFrame {
	BookDaoImpl bdi = new BookDaoImpl();
	String[][] date = null;
	String[][] date2 = null;
	BookServiceImpl bsi = new BookServiceImpl();
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JButton jButton5;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	private javax.swing.JTextField jTextField4;
	private javax.swing.JTextField jTextField5;

	public WinReadertypeset() {
		initComponents();
		setVisible(true);
	}

	private void initComponents() {
		setTitle("读者类型管理");
		jLabel1 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jTextField2 = new javax.swing.JTextField();
		jTextField3 = new javax.swing.JTextField();
		jTextField4 = new javax.swing.JTextField();
		jTextField5 = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();
		jButton5 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(new java.awt.Rectangle(500, 270, 668, 52));

		jLabel1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
		jLabel1.setText("读者类型");

		jTextField1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jButton1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton1.setText("查询");

		jTable1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
		jTable1.setModel(new javax.swing.table.DefaultTableModel(bdi.getReaderType(),
				new String[] { "读者类型编号", "读者类型名称", "可借图书数量", "可解图书期限" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jTable1.setRowHeight(30);
		jScrollPane1.setViewportView(jTable1);

		jLabel2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel2.setText("读者类型编号");

		jLabel3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel3.setText("可解图书数量");

		jLabel4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel4.setText("读者类型名称");

		jLabel5.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel5.setText("可借图书期限");

		jTextField2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jTextField3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jTextField4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jTextField5.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jButton2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton2.setText("修改");

		jButton3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton3.setText("删除");

		jButton4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton4.setText("退出");

		jButton5.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton5.setText("添加");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
										.addComponent(jScrollPane1)
										.addGroup(layout.createSequentialGroup()
												.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 101,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 324,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(
														jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 147,
														javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addGroup(layout.createSequentialGroup().addGroup(layout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createSequentialGroup().addGroup(layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
												.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(
														jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
												.addGroup(layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(layout.createSequentialGroup().addGap(10, 10, 10)
																.addComponent(jTextField2,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 193,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(layout.createSequentialGroup().addPreferredGap(
																javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(jTextField3,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 193,
																		javax.swing.GroupLayout.PREFERRED_SIZE))))
										.addGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												layout.createSequentialGroup().addComponent(jButton5).addGap(18, 18, 18)
														.addComponent(jButton2)))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(layout.createSequentialGroup().addGroup(layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
														.addPreferredGap(
																javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
														.addGroup(layout
																.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.LEADING)
																.addComponent(jTextField4,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 193,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jTextField5,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 193,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
												.addGroup(layout.createSequentialGroup().addComponent(jButton3)
														.addGap(32, 32, 32).addComponent(jButton4)))))
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addGap(26, 26, 26)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
								.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
								.addComponent(jTextField1).addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(41, 41, 41)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
										javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
										javax.swing.GroupLayout.PREFERRED_SIZE))
						.addGap(57, 57, 57)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jButton2).addComponent(jButton4).addComponent(jButton3)
								.addComponent(jButton5))
						.addGap(24, 24, 24)));
		jTable1.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {			}
			@Override
			public void mousePressed(MouseEvent arg0) {			}
			@Override
			public void mouseExited(MouseEvent arg0) {			}
			@Override
			public void mouseEntered(MouseEvent arg0) {			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//获取表格中被选中的行
				int row = jTable1.getSelectedRow();
				date = bdi.getReaderType();
				//设置文本框的内容 
				jTextField3.setText(date[row][0]);
				jTextField5.setText(date[row][1]);
				jTextField2.setText(date[row][2]);
				jTextField4.setText(date[row][3]);
			}
		});
		pack();
		jButton1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//查阅
				date2 = bdi.getReaderType(jTextField1.getText());
				if (date2 == null) {
					new WinTip("未找到该种类，换一下试试");
				} else {
					DefaultTableModel dataModel = new DefaultTableModel(date2,
							new String[] { "读者类型编号", "读者类型名称", "可借图书数量", "可解图书期限" });
					jTable1.setModel(dataModel);
					
					jTable1.addMouseListener(new MouseListener() {
						@Override
						public void mouseReleased(MouseEvent arg0) {			}
						@Override
						public void mousePressed(MouseEvent arg0) {			}
						@Override
						public void mouseExited(MouseEvent arg0) {			}
						@Override
						public void mouseEntered(MouseEvent arg0) {			}
						@Override
						public void mouseClicked(MouseEvent arg0) {
							//获取表格中被选中的行
							int row = jTable1.getSelectedRow();
							//date = bdi.getReaderType();
							//设置文本框的内容 
							jTextField3.setText(date2[row][0]);
							jTextField5.setText(date2[row][1]);
							jTextField2.setText(date2[row][2]);
							jTextField4.setText(date2[row][3]);
						}
					});
				}
			}
		});
		jButton2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//修改
				int row = jTable1.getSelectedRow();
				date = bdi.getReaderType();
				String aa = date[row][0];
				String q = jTextField3.getText();
				String w = jTextField5.getText();
				String s = jTextField2.getText();
				String t = jTextField4.getText();
				int rows = bsi.ChangeReaderType(aa, q, w, s, t);
				if(rows == 1) {
					WinReadertypeset.this.dispose();
					new WinReadertypeset();
					new WinTip("修改成功");
				}else {
					new WinTip("修改失败");
				}
			}
		});
		jButton3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//删除
				int row = jTable1.getSelectedRow();
				date = bdi.getReaderType();
				String aa = date[row][0];
				int rows = bsi.DeleteReaderType(aa);
				if(rows == 1) {
					WinReadertypeset.this.dispose();
					new WinReadertypeset();
					new WinTip("删除成功");
				}else {
					new WinTip("删除失败");
				}
			}
		});
		jButton4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				WinReadertypeset.this.dispose();
			}
		});
		jButton5.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//添加
				String q = jTextField3.getText();
				String w = jTextField5.getText();
				String s = jTextField2.getText();
				String t = jTextField4.getText();
				int row = bsi.ReaderTypeAdd(q, w, s, t);
				if(row == 1) {
					WinReadertypeset.this.dispose();
					new WinReadertypeset();
					new WinTip("添加成功");
				}else {
					new WinTip("添加失败");
				}
			}
		});

	}

	public static void main(String args[]) {
		new WinReadertypeset();
	}

}
